//
//  UIImage+WaterMark.m
//  自定义相册
//
//  Created by DongSen on 2018/7/9.
//  Copyright © 2018年 beok. All rights reserved.
//

#import <CoreImage/CoreImage.h>
#import "UIImage+MultiFormat.h"

#import "UIImage+WaterMark.h"

@implementation UIImage (WaterMark)
- (UIImage *)imageWithWaterMask:(UIImage*)mask
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0)
    {
        UIGraphicsBeginImageContextWithOptions([self size], NO, 0.0); // 0.0 for scale means "scale for device's main screen".
    }
#else
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0)
    {
        UIGraphicsBeginImageContext([self size]);
    }
#endif
    //原图
    [self drawInRect:CGRectMake(0, 0,self.size.width,self.size.height)];
    UIImage * maskImage = [UIImage imageCompressForWidthScale:mask targetWidth:self.size.width];
    //水印图
    [mask drawInRect:CGRectMake(0, self.size.height-maskImage.size.height,maskImage.size.width,maskImage.size.height)];
    UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newPic;
}

+(UIImage *)imageCompressForWidthScale:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{
    
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = defineWidth;
    CGFloat targetHeight = height / (width / targetWidth);
    CGSize size = CGSizeMake(targetWidth, targetHeight);
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
    
    if(CGSizeEqualToSize(imageSize, size) == NO){
        
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        
        if(widthFactor > heightFactor){
            scaleFactor = widthFactor;
        }
        else{
            scaleFactor = heightFactor;
        }
        scaledWidth = width * scaleFactor;
        scaledHeight = height * scaleFactor;
        
        if(widthFactor > heightFactor){
            
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
            
        }else if(widthFactor < heightFactor){
            
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
    
    UIGraphicsBeginImageContext(size);
    
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    if(newImage == nil){
        
        NSLog(@"scale image fail");
    }
    UIGraphicsEndImageContext();
    return newImage;
}



-(UIImage *)compressImage
{
    NSData * imageData = UIImageJPEGRepresentation(self,1);
    //    NSData * imageData = UIImagePNGRepresentation(image); 为图片大小不易过大 使用JPG格式图片
    NSInteger  jpglength = [imageData length]/1000;
    // 系统本身压缩方式
    if (jpglength>400)
    {
        float resize = 400.00/ jpglength;
        imageData =UIImageJPEGRepresentation(self, resize);
    }
    UIImage * transimage = [UIImage sd_imageWithData:imageData];
    return transimage;
}
- (UIImage *)compressImagewithSize:(CGSize)size{
    UIGraphicsBeginImageContext(size);
    //    float width = image.size.width/kMainScreenWidth;
    //    float height = image.size.height/kMainScreenHeight;
    //    float multiple = MAX(width, height);
    // 第二种通过绘图将image的尺寸限制在屏幕大小以内
    [self drawInRect:CGRectMake(0,0,size.width,size.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

+ (UIImage *)ownercompressImage:(UIImage *)image toByte:(NSUInteger)maxLength {
    // Compress by quality
    CGFloat compression = 1;
    NSData *data = UIImageJPEGRepresentation(image, compression);
    if (data.length < maxLength) return image;
    
    CGFloat max = 1;
    CGFloat min = 0;
    for (int i = 0; i < 6; ++i) {
        compression = (max + min) / 2;
        data = UIImageJPEGRepresentation(image, compression);
        if (data.length < maxLength * 0.9) {
            min = compression;
        } else if (data.length > maxLength) {
            max = compression;
        } else {
            break;
        }
    }
    UIImage *resultImage = [UIImage imageWithData:data];
    if (data.length < maxLength) return resultImage;
    
    // Compress by size
    NSUInteger lastDataLength = 0;
    while (data.length > maxLength && data.length != lastDataLength) {
        lastDataLength = data.length;
        CGFloat ratio = (CGFloat)maxLength / data.length;
        CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
                                 (NSUInteger)(resultImage.size.height * sqrtf(ratio)));         UIGraphicsBeginImageContext(size);
        [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
        resultImage = UIGraphicsGetImageFromCurrentImageContext();
        data = UIImageJPEGRepresentation(resultImage, compression);
        resultImage = [UIImage imageWithData:data];
        UIGraphicsEndImageContext();
    }
    return resultImage;
}

- (UIImage *)clipImage
{
    CGSize originImageSize = self.size;
    CGFloat originViewW = originImageSize.width;
    CGFloat originViewH = originImageSize.height;
    CGFloat clipW = MIN(originViewW, originViewH);
    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], CGRectMake(0, 0, clipW, clipW));
    UIImage *thumbScale = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return thumbScale;
}

@end
